package com.dlc.shop.seckill.common.listener;

import com.dlc.shop.bean.dto.OrderRefundDto;
import com.dlc.shop.bean.event.OrderRefundSuccessEvent;
import com.dlc.shop.bean.model.OrderItem;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.util.RedisUtil;
import com.dlc.shop.seckill.common.service.SeckillOrderService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 退款订单监听
 * @author lhd
 */
@Component("seckillOrderRefundListener")
@AllArgsConstructor
public class OrderRefundListener {

    private final SeckillOrderService seckillOrderService;


    /**
     * 退还用户此笔退款订单使用的优惠券
     */
    @EventListener(OrderRefundSuccessEvent.class)
    public void seckillOrderRefundSuccessEvent(OrderRefundSuccessEvent event) {
        OrderRefundDto orderRefundDto = event.getOrderRefundDto();
        //获取退款的所有订单项
        if (CollectionUtils.isEmpty(orderRefundDto.getOrderItems())) {
            return;
        }
        // TODO stock
        OrderItem orderItem = orderRefundDto.getOrderItems().get(0);
        // 清除库存的缓存
        RedisUtil.del(Constant.SECKILL_SKU_STOCKS_PREFIX + orderItem.getActivityId());

    }
}
